package com.librarysys.controller;

import com.librarysys.common.Const;
import com.librarysys.common.Result;
import com.librarysys.dao.PurviewMapper;
import com.librarysys.entity.Manager;
import com.librarysys.entity.Purview;
import com.librarysys.service.IManagerService;
import com.librarysys.vo.ManagerPurviewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yuhui on 2017/7/11.
 */
@Controller
@RequestMapping("/manager")
public class ManagerController {

    @Autowired
    private IManagerService iManagerService;


    /**
     * 用户登陆
     * @param name
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public String login(String name, String password, HttpSession session, HttpServletRequest request){
        Result login = iManagerService.login(name, password);
        if(login.getCode()==Result.SUCESS){
            session.setAttribute(Const.CURRENT_USER,login.getData());
            return "/index";
        }else{
            request.setAttribute("login_msg",login.getMsg());
            return "login";
        }
    }

    /**
     * 用户注销
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String  logout(HttpSession session){
        session.invalidate();
        return "login";
    }

    /**
     * 用户名是否存在
     * @param name
     * @return
     */
    @RequestMapping("/check")
    @ResponseBody
    public Boolean check(String name){
        return iManagerService.check(name);
    }

    @RequestMapping("/check_pwd")
    @ResponseBody
    public Boolean check_pwd(HttpSession session, String oldpwd){
        Manager attribute = (Manager) session.getAttribute(Const.CURRENT_USER);
        if(attribute==null){
            return false;
        }else{
            Result login = iManagerService.login(attribute.getName(), oldpwd);
            if(login.isSuccess()){
                return true;
            }else{
                return false;
            }
        }
    }

    /**
     * 修改密码
     * @param name
     * @param oldpwd
     * @param pwd
     * @param request
     * @return
     */
    @RequestMapping("/modify")
    public String modify(String name,String oldpwd,String pwd,HttpServletRequest request){
        Result modify = iManagerService.modify(name, oldpwd, pwd);
        request.setAttribute("msg",modify.getMsg());
        return "pwd_Modify";
    }

    @RequestMapping("/list")
    public String listManager(Model model){
        Result result = iManagerService.listManager();
        List<ManagerPurviewVO> list=(List<ManagerPurviewVO>)result.getData();
        model.addAttribute("manager",list);
        return "manager";
    }

    @RequestMapping("/get_all")
    @ResponseBody
    public List<ManagerPurviewVO> listManager(){
        Result result = iManagerService.listManager();
        List<ManagerPurviewVO> list=(List<ManagerPurviewVO>)result.getData();
        return list;
    }

    @RequestMapping("/add")
    public String addManager(Manager manager,Model model,HttpSession session){
        Manager attribute = (Manager) session.getAttribute(Const.CURRENT_USER);
        if(attribute==null || attribute.getRole()!=Const.Role.SUPER_ADMIN){
            model.addAttribute("msg","没有权限");
            return "redirect:/manager/list";
        }
        Result result = iManagerService.addManager(manager);
        model.addAttribute("model",result);
        return "redirect:/manager/list";
    }

    @RequestMapping("/del")
    public String delManager(Integer id,HttpServletRequest request){
        Manager attribute = (Manager) request.getSession().getAttribute(Const.CURRENT_USER);
        if(attribute==null || attribute.getRole()!=Const.Role.SUPER_ADMIN){
            request.setAttribute("msg","没有权限");
            return "redirect:/manager/list";
        }
        Result result = iManagerService.delManager(id);
        request.setAttribute("msg",result.getMsg());
        return "/manager/list";
    }

    @RequestMapping("/pruview")
    public String addPurview(Integer id,Integer[]sysset,HttpServletRequest request){
        Manager attribute = (Manager) request.getSession().getAttribute(Const.CURRENT_USER);
        if(attribute==null || attribute.getRole()!=Const.Role.SUPER_ADMIN){
            request.setAttribute("msg","没有权限");
            return "redirect:/manager/list";
        }else {
            Boolean[] booleans={false,false,false,false,false};
            if(sysset!=null){
                for(Integer x:sysset){
                    booleans[x-1]=true;
                }
            }
            Result result = iManagerService.addPurview(new Purview(id,booleans[0],booleans[1],booleans[2],booleans[3],booleans[4]));
            request.setAttribute("msg",result.getMsg());
            return "/manager/list";
        }

    }
}
